unit Main;

interface

uses
{$IFDEF CLR}
  System.ComponentModel,
{$ENDIF}
  Classes, SysUtils, DBClient, Provider, DBXpress, FMTBcd, DB, SqlExpr, DBXInfExt,
{$IFDEF LINUX}
  QControls, QForms, QDialogs, QStdCtrls, QComCtrls, QActnList, QGrids, QDBGrids, QDBCtrls, QExtCtrls,
{$ELSE}
  Controls, Forms, Dialogs, StdCtrls, ComCtrls, ActnList, Grids, DBGrids, DBCtrls, ExtCtrls;
{$ENDIF}

type
  TFormMain = class(TForm)
    sqlConnection: TSQLConnection;
    pageControl: TPageControl;
    tabsheetConnection: TTabSheet;
    tabsheetCustomers: TTabSheet;
    memoConnectionParams: TMemo;
    labelConnectionParameters: TLabel;
    buttonConnect: TButton;
    buttonDisconnect: TButton;
    actionList: TActionList;
    actionConnect: TAction;
    actionDisconnect: TAction;
    labelUserName: TLabel;
    editUserName: TEdit;
    labelPassword: TLabel;
    editPassword: TEdit;
    queryCustomer: TSQLQuery;
    queryCustomercustomer_num: TIntegerField;
    queryCustomercustomer_type: TStringField;
    queryCustomercustomer_name: TBlobField;
    queryCustomercustomer_loc: TIntegerField;
    queryCustomercontact_dates: TBlobField;
    queryCustomercust_discount: TFMTBCDField;
    queryCustomercredit_status: TStringField;
    providerCustomer: TDataSetProvider;
    datasetCustomer: TClientDataSet;
    datasetCustomercustomer_num: TIntegerField;
    datasetCustomercustomer_type: TStringField;
    datasetCustomercustomer_name: TBlobField;
    datasetCustomercustomer_loc: TIntegerField;
    datasetCustomercontact_dates: TBlobField;
    datasetCustomercust_discount: TFMTBCDField;
    datasetCustomercredit_status: TStringField;
    datasetCustomercustomername: TStringField;
    datasetCustomercontactdates: TStringField;
    datasourceCustomer: TDataSource;
    gridCustomer: TDBGrid;
    memoCustomerName: TDBMemo;
    memoContactDates: TDBMemo;
    labelCustomerName: TLabel;
    dbnavigator: TDBNavigator;
    labelContactDates: TLabel;
    buttonOpen: TButton;
    buttonClose: TButton;
    actionOpen: TAction;
    actionClose: TAction;
    bevelCustomer: TBevel;
    labelDatabase: TLabel;
    editDatabase: TEdit;
    labelServerVersion: TLabel;
    procedure actionConnectExecute(Sender: TObject);
    procedure actionDisconnectExecute(Sender: TObject);
    procedure actionListUpdate(Action: TBasicAction; var Handled: Boolean);
    procedure datasetCustomerCalcFields(DataSet: TDataSet);
    procedure datasetCustomerApplyUpdates(DataSet: TDataSet);
    procedure actionOpenExecute(Sender: TObject);
    procedure actionCloseExecute(Sender: TObject);
    procedure providerCustomerAfterUpdateRecord(Sender: TObject;
      SourceDS: TDataSet; DeltaDS: TCustomClientDataSet;
      UpdateKind: TUpdateKind);
    procedure datasetCustomerReconcileError(DataSet: TCustomClientDataSet;
      E: EReconcileError; UpdateKind: TUpdateKind;
      var Action: TReconcileAction);
  end;

var
  FormMain: TFormMain;

implementation

{$R *.dfm}

procedure TFormMain.actionConnectExecute(Sender: TObject);
begin
{$IFDEF LINUX}
  sqlConnection.LibraryName := 'lxdbexpinfpro.so';
{$ENDIF}
  sqlConnection.Params.Values['HostName'] := memoConnectionParams.Text;
  sqlConnection.Params.Values['Database'] := editDatabase.Text;
  sqlConnection.Params.Values['User_Name'] := editUserName.Text;
  sqlConnection.Params.Values['Password'] := editPassword.Text;
  sqlConnection.Open;
  labelServerVersion.Caption := 'Connected to: ' + IInformixConnection(
    sqlConnection.SQLConnection).ServerVersion;
end;

procedure TFormMain.actionDisconnectExecute(Sender: TObject);
begin
  sqlConnection.Close;
  labelServerVersion.Caption := 'Not connected';
end;

procedure TFormMain.actionListUpdate(Action: TBasicAction;
  var Handled: Boolean);
begin
  actionConnect.Enabled := not sqlConnection.Connected;
  actionDisconnect.Enabled := sqlConnection.Connected;
  actionOpen.Enabled := not datasetCustomer.Active;
  actionClose.Enabled := datasetCustomer.Active;
end;

procedure TFormMain.datasetCustomerCalcFields(DataSet: TDataSet);
begin
  datasetCustomercustomername.Value := datasetCustomercustomer_name.AsString;
  datasetCustomercontactdates.Value := datasetCustomercontact_dates.AsString;
end;

procedure TFormMain.datasetCustomerApplyUpdates(DataSet: TDataSet);
begin
  datasetCustomer.ApplyUpdates(-1);
end;

procedure TFormMain.actionOpenExecute(Sender: TObject);
begin
  datasetCustomer.Open;
end;

procedure TFormMain.actionCloseExecute(Sender: TObject);
begin
  datasetCustomer.Close;
end;

procedure TFormMain.providerCustomerAfterUpdateRecord(Sender: TObject;
  SourceDS: TDataSet; DeltaDS: TCustomClientDataSet;
  UpdateKind: TUpdateKind);
begin
  if UpdateKind = ukInsert then
    DeltaDS.FieldByName('customer_num').NewValue := IInformixConnection(
      sqlConnection.SQLConnection).SQLCA.sqlerrd[1];
end;

procedure TFormMain.datasetCustomerReconcileError(
  DataSet: TCustomClientDataSet; E: EReconcileError;
  UpdateKind: TUpdateKind; var Action: TReconcileAction);
begin
  Application.ShowException(E);
  Action := raCancel;
end;

end.
